package com.shundr.user.http;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.shundr.cargo.model.CargoInfo;
import com.shundr.common.model.CheckInfo;
import com.shundr.common.util.EncryptionUtils;
import com.shundr.common.util.IpUtils;
import com.shundr.frame.constant.LCommands;
import com.shundr.frame.constant.LContext;
import com.shundr.frame.http.HttpResult;
import com.shundr.frame.http.LHttpRequest;
import com.shundr.frame.http.LHttpRequest.RequestCompleteListener;
import com.shundr.user.model.UserInfo;

public class UserRequest extends LHttpRequest implements
		RequestCompleteListener {

	public static final int LOGIN_SUCCESSED = 101;// 登录成功
	public static final int CHECK_PHONE_SUCCESSED = 2;// 检测手机号成功
	public static final int GET_AUTH_CODE_SUCCESSED = 3;// 获取验证码成功
	public static final int CHECK_CAPTCHA_SUCCESSED = 4;// 验证码成功
	public static final int REGISTER_SUCCESSED = 5;// 注册成功
	public static final int DRIVER_AUTH_COMMIT_SUCCESSED = 6;// 提交司机认证信息成功
	public static final int GET_USER_INFO_SUCCESSED = 7;// 获取用户信息
	public static final int GET_VALIDATION_STATUS_SUCCESSED = 8;// 获取认证状态
	public static final int GET_QueryNumValidationByUser_SUCCESSED = 9;// 获取可打电话次数
	public static final int GRAB_ORDER_SUCCESSED = 10;// 获取电话号码
	public static final int GET_USER_DETAIL_INFO_SUCCESSED = 11;// 获取用户详细信息

	public static final int RESER_USERPSW_SUCCESSED = 12;// 修改密码成功
	public static final int COMMIT_USER_HEAD_SUCCESSED = 13;// 修改头像成功
	public static final int RESER_USER_OLDPSW_SUCCESSED = 14;//旧密码
	public static final int GET_CONFIRM_USERINFO_SUCCESSED = 15;//旧密码
	public static final int UPDATE_USERPHONE_SUCCESSED = 16;//修改手机号
	
	public static final int CHECK_IN_SUCCESSED = 17;//签到成功
	public static final int INVITE_REGISTER_SUCCESSED = 18;//邀请注册成功
	public static final int GET_INVITE_SUCCESSED = 19;//邀请注册列表
	
	public static final int GET_INTEGRAL_TOTAL_SUCCESSED = 20;//我的积分总额
	public static final int GET_INTEGRAL_LIST_SUCCESSED = 21;//我的积分记录
	
	public static final int COMMIT_RE_AUTH_SUCCESSED = 22;// 重新提交司机认证信息成功
	
	public static final int GET_INTEGRAL_DETAIL_SUCCESSED = 26;//我的积分详情
	public static final int GET_EXCHANGE_LIST_SUCCESSED = 27;//我的兑换记录
	
	public static final int APPLY_EXCHANGE_SUCCESSED = 28;//申请兑换成功
	
	private Handler mHandler;
	private Message mMessage;
	private Context mContext;

	private Map<String, Object> param;
	private String command;

	public UserRequest(Context context, Handler handler) {
		this.mContext = context;
		this.mHandler = handler;
		this.mMessage = mHandler.obtainMessage();
		this.setRequestCompleteListener(this);
	}

	/**
	 * 
	 * login(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param phone
	 * @param password
	 *            void
	 * @exception
	 * @since 1.0.0
	 */
	public void login(String phone, String password) {
		param = new HashMap<String, Object>();
		String loginIp = IpUtils.getWifiIpAddress(mContext);
		if (loginIp == null) {
			loginIp = IpUtils.getLocalIpAddress(mContext);
		}
		if (loginIp == null) {
			loginIp = "null";
		}
		String loginSerialId = IpUtils.getIMEI(mContext);

		param.put("userpsw", EncryptionUtils.SHA1(password));
		param.put("loginIp", loginIp);
		param.put("loginIpPlace", LContext.mAddress);
		param.put("userphone", phone);
		param.put("loginPlatformTag", "0");
		param.put("loginSerialId", loginSerialId);
		command = LCommands.USER_LOGIN;
		requestByGet(param);
	}

	/**
	 * 检测手机号是否可注册 checkPhone(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param phone
	 *            void
	 * @exception
	 * @since 1.0.0
	 */
	public void checkPhone(String phone) {
		param = new HashMap<String, Object>();
		param.put("phone", phone);
		command = LCommands.CHECK_PHONE + "/" + phone;
		requestByGet(param);
	}
	
	/**
	 * 验证旧密码
	 * @param psw
	 */
	public void getConfirmUserInfo(String psw) {
		param = new HashMap<String, Object>();
		param.put("userpsw", EncryptionUtils.SHA1(psw));
		command = LCommands.GET_CONFIRM_USERINFO;
		requestByGet(param);
	}

	/**
	 * 修改密码
	 * 
	 * @param psw
	 */
	public void resetUserOldPsw(String psw) {
		param = new HashMap<String, Object>();
		param.put("userpsw", EncryptionUtils.SHA1(psw));
		command = LCommands.RESET_USER_OLDPSW;
		requestByPost(param);
	}
	
	/***
	 * 修改绑定手机
	 * @param phone
	 */
	public void updateUserPhone(String phone) {
		param = new HashMap<String, Object>();
		param.put("userphone", phone);
		command = LCommands.UPDATE_USERPHONE;
		requestByPost(param);
	}

	/**
	 * 获取验证码 getAuthCode(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param phone
	 * @param type
	 *            void
	 * @exception
	 * @since 1.0.0
	 */
	public void getAuthCode(String phone, String type) {
		param = new HashMap<String, Object>();
		String ip = IpUtils.getWifiIpAddress(mContext);
		if (ip == null) {
			ip = IpUtils.getLocalIpAddress(mContext);
		}
		if (ip == null) {
			ip = "null";
		}
		param.put("phone", phone);
		param.put("type", type);
		param.put("ip", ip);
		command = LCommands.GET_AUTH_CODE;
		requestByGet(param);
	}

	/**
	 * 
	 * checkCaptcha(这里用一句话描述这个方法的作用) 检测验证码是否匹配 (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param phone
	 * @param captcha
	 *            void
	 * @exception
	 * @since 1.0.0
	 */
	public void checkCaptcha(String phone, String captcha) {
		param = new HashMap<String, Object>();
		param.put("phone", phone);
		param.put("captcha", captcha);
		command = LCommands.CHECK_GETCAPTCHA;
		requestByGet(param);
	}

	/**
	 * 注册 register(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param type
	 * @param phone
	 * @param userpsw
	 * @param captcha
	 *            void
	 * @exception
	 * @since 1.0.0
	 */
	public void register(String type, String phone, String userpsw,
			String captcha) {
		param = new HashMap<String, Object>();
		param.put("usertype", type);
		param.put("phone", phone);
		param.put("userpsw", EncryptionUtils.SHA1(userpsw));
		param.put("captcha", captcha);
		command = LCommands.REGISTER;
		requestByPost(param);
	}

	/**
	 * 重置密码
	 * 
	 * @param phone
	 * @param userpsw
	 */
	public void resetUserPsw(String phone, String userpsw) {
		param = new HashMap<String, Object>();
		param.put("phone", phone);
		param.put("userpsw", EncryptionUtils.SHA1(userpsw));
		command = LCommands.RESETPSW;
		requestByPost(param);
	}

	/**
	 * 提交司机个人认证 commitDriverAuth(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param name
	 * @param citizenNum
	 * @param fileObjectListJson
	 *            void
	 * @exception
	 * @since 1.0.0
	 */
	public void commitDriverAuth(String name, String citizenNum,
			String fileObjectListJson) {
		param = new HashMap<String, Object>();
		param.put("name", name);
		param.put("citizenNum", citizenNum);
		param.put("adminAccessToken", LContext.ADMIN_TOKEN);
		param.put("fileObjectListJson", fileObjectListJson);
		command = LCommands.COMMIT_DRIVER_AUTH_COMMIT;
		requestByPost(param);
	}
	
	/**
	 * 重新提交认证
	 * commitReAuth(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param name
	 * @param citizenNum
	 * @param fileObjectListJson
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void commitReAuth(String name, String citizenNum,
			String fileObjectListJson) {
		param = new HashMap<String, Object>();
		param.put("name", name);
		param.put("citizenNum", citizenNum);
		param.put("fileObjectListJson", fileObjectListJson);
		command = LCommands.COMMIT_RE_AUTH_COMMIT;
		requestByPost(param);
	}

	/**
	 * 修改头像 commitUserHead(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param fileObjectListJson
	 *            void
	 * @exception
	 * @since 1.0.0
	 */
	public void commitUserHead(String fileObjectListJson) {
		param = new HashMap<String, Object>();
		param.put("fileObjectListJson", fileObjectListJson);
		command = LCommands.COMMIT_USER_HEAD;
		requestByPost(param);
	}

	/**
	 * 获取用户信息
	 */
	public void getUserInfo() {
		param = new HashMap<String, Object>();
		command = LCommands.GET_USER_INFO;
		requestByGet(param);
	}

	/**
	 * 获取认证状态
	 */
	public void getValidationStatus() {
		param = new HashMap<String, Object>();
		command = LCommands.GET_VALIDATION_STATUS;
		requestByPost(param);
	}

	/**
	 * 得到可打电话次数
	 */
	public void grabOrder(String cargoId) {
		param = new HashMap<String, Object>();
		param.put("cargoId", cargoId);
		command = LCommands.GRAB_ORDER;
		requestByGet(param);
	}

	/**
	 * 得到可打电话次数
	 */
	public void getQueryNumValidationByUser() {
		param = new HashMap<String, Object>();
		command = LCommands.GET_QueryNumValidationByUser;
		requestByGet(param);
	}

	/**
	 * 
	 * getUserInfoByToken(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选) void
	 * 
	 * @exception
	 * @since 1.0.0
	 */
	public void getUserInfoByToken() {
		param = new HashMap<String, Object>();
		command = LCommands.GET_USER_INFO;
		requestByGet(param);
	}
	
	/**
	 * 签到
	 * checkIn(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void checkIn() {
		param = new HashMap<String, Object>();
		command = LCommands.CHECK_IN;
		requestByPost(param);
	}
	
	/**
	 * 邀请注册
	 * inviteRegister(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param name
	 * @param phone
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void inviteRegister(String name,String phone) {
		param = new HashMap<String, Object>();
		param.put("name", name);
		param.put("phone", phone);
		command = LCommands.INVITE_REGISTER;
		requestByPost(param);
	}
	
	/**
	 * 我的邀请列表
	 * getInviteList(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param pageNo
	 * @param pageSize
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getInviteList(int pageNo,int pageSize) {
		param = new HashMap<String, Object>();
		param.put("pageIndex", pageNo);
		param.put("pageSize", pageSize);
		command = LCommands.GET_INVITE_LIST;
		requestByGet(param);
	}
	
	/**
	 * 获取我的积分总额
	 * getIntegralTotal(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getCreditsTotal() {
		param = new HashMap<String, Object>();
		command = LCommands.GET_INTEGRAL_TOTAL;
		requestByGet(param);
	}
	
	/**
	 * 我的积分记录
	 * getIntegralList(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param pageNo
	 * @param pageSize
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getCreditsList(int pageNo,int pageSize) {
		param = new HashMap<String, Object>();
		param.put("pageIndex", pageNo);
		param.put("pageSize", pageSize);
		command = LCommands.GET_INTEGRAL_LIST;
		requestByGet(param);
	}
	
	/**
	 * 注销用户
	 * logout(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void logout() {
		param = new HashMap<String, Object>();
		command = LCommands.USER_LOGOUT;
		requestByGet(param);
	}
	
	/**
	 * 我的积分详情
	 * getCreditsDetail(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getCreditsDetail() {
		param = new HashMap<String, Object>();
		command = LCommands.GET_INTEGRAL_DETAIL;
		requestByGet(param);
	}
	
	/**
	 * 兑换记录
	 * getExchangeList(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param pageNo
	 * @param pageSize
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getExchangeList(int pageNo,int pageSize) {
		param = new HashMap<String, Object>();
		param.put("pageIndex", pageNo);
		param.put("pageSize", pageSize);
		command = LCommands.GET_EXCHANGE_LIST;
		requestByGet(param);
	}
	
	/**
	 * 
	 * applyExchange(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param id
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void applyExchange(String type) {
		param = new HashMap<String, Object>();
		param.put("intergralType", type);
		command = LCommands.APPLY_EXCHANGE;
		requestByPost(param);
	}

	@Override
	protected void initParams() {
		super.initParams();
		mParam = param;
		mCommand = command;
		mBaseContext = mContext;
	}

	@Override
	public void requestSuccessed(HttpResult result) {
		if (mHandler == null)
			return;
		if (command.equals(LCommands.USER_LOGIN)) {
			mMessage.what = LOGIN_SUCCESSED;
			mMessage.obj = result.getResult();
			mHandler.sendMessage(mMessage);
		} else if (command.contains(LCommands.CHECK_PHONE)) {
			mMessage.what = CHECK_PHONE_SUCCESSED;
			mHandler.sendMessage(mMessage);
		} else if (command.equals(LCommands.GET_AUTH_CODE)) {
			mMessage.what = GET_AUTH_CODE_SUCCESSED;
			mHandler.sendMessage(mMessage);
		} else if (command.equals(LCommands.CHECK_GETCAPTCHA)) {
			mMessage.what = CHECK_CAPTCHA_SUCCESSED;
			mHandler.sendMessage(mMessage);
		}else if (command.equals(LCommands.REGISTER)) {
			mMessage.what = REGISTER_SUCCESSED;
			mMessage.obj = result.getDescription();
			mHandler.sendMessage(mMessage);
		}else if (command.equals(LCommands.COMMIT_DRIVER_AUTH_COMMIT)) {
			mMessage.what = DRIVER_AUTH_COMMIT_SUCCESSED;
			// mMessage.obj = UserParser.parserUpload(result.getResult());
			mHandler.sendMessage(mMessage);
		}else if (command.equals(LCommands.COMMIT_USER_HEAD)) {
			mMessage.what = COMMIT_USER_HEAD_SUCCESSED;
			// mMessage.obj = UserParser.parserUpload(result.getResult());
			mHandler.sendMessage(mMessage);
		} else if (command.equals(LCommands.GET_VALIDATION_STATUS)) {
			mMessage.what = GET_VALIDATION_STATUS_SUCCESSED;
			String idValiStatus = UserParser.parserUserStatus(result
					.getResult());
			mMessage.obj = idValiStatus;
			mHandler.sendMessage(mMessage);
		}else if (command.equals(LCommands.GET_QueryNumValidationByUser)) {
			mMessage.what = GET_QueryNumValidationByUser_SUCCESSED;
			String num = result.getResult();
			mMessage.obj = num;
			mHandler.sendMessage(mMessage);
		}else if (command.equals(LCommands.GRAB_ORDER)) {
			mMessage.what = GRAB_ORDER_SUCCESSED;
			CargoInfo infos = UserParser.parserCargoInfo(result.getResult());
			mMessage.obj = infos;
			mHandler.sendMessage(mMessage);
		}else if (command.equals(LCommands.GET_USER_INFO)) {
			mMessage.what = GET_USER_DETAIL_INFO_SUCCESSED;
			UserInfo info = UserParser.parserUserDetailInfo(result.getResult());
			mMessage.obj = info;
			mHandler.sendMessage(mMessage);
		} else if (command.equals(LCommands.RESETPSW)) {
			mMessage.what = RESER_USERPSW_SUCCESSED;
			mMessage.obj = result.getDescription();
			mHandler.sendMessage(mMessage);
		} else if (command.equals(LCommands.RESET_USER_OLDPSW)) {
			mMessage.what = RESER_USER_OLDPSW_SUCCESSED;
			mMessage.obj = result.getDescription();
			mHandler.sendMessage(mMessage);
		}else if (command.equals(LCommands.GET_CONFIRM_USERINFO)) {
			mMessage.what = GET_CONFIRM_USERINFO_SUCCESSED;
			mMessage.obj = result.getDescription();
			mHandler.sendMessage(mMessage);
		}else if (command.equals(LCommands.UPDATE_USERPHONE)) {
			mMessage.what = UPDATE_USERPHONE_SUCCESSED;
			mMessage.obj = result.getDescription();
			mHandler.sendMessage(mMessage);
		}else if (command.equals(LCommands.CHECK_IN)) {
			mMessage.what = CHECK_IN_SUCCESSED;
			CheckInfo info = UserParser.parserCheckInfo(result.getResult());
			mMessage.obj = info;
			mHandler.sendMessage(mMessage);
		}else if (command.equals(LCommands.INVITE_REGISTER)) {
			mMessage.what = INVITE_REGISTER_SUCCESSED;
			mMessage.obj = result.getDescription();
			mHandler.sendMessage(mMessage);
		}else if (command.equals(LCommands.GET_INVITE_LIST)) {
			mMessage.what = GET_INVITE_SUCCESSED;
			mMessage.obj = UserParser.parserInviteList(result.getResult());
			mHandler.sendMessage(mMessage);
		}else if (command.equals(LCommands.GET_INTEGRAL_TOTAL)) {
			mMessage.what = GET_INTEGRAL_TOTAL_SUCCESSED;
			mMessage.obj = result.getResult();
			mHandler.sendMessage(mMessage);
		}else if (command.equals(LCommands.GET_INTEGRAL_LIST)) {
			mMessage.what = GET_INTEGRAL_LIST_SUCCESSED;
			mMessage.obj = UserParser.parserIntegralList(result.getResult());
			mHandler.sendMessage(mMessage);
		}else if (command.equals(LCommands.COMMIT_RE_AUTH_COMMIT)) {
			mMessage.what = COMMIT_RE_AUTH_SUCCESSED;
			mHandler.sendMessage(mMessage);
		}else if (command.equals(LCommands.GET_INTEGRAL_DETAIL)) {
			mMessage.what = GET_INTEGRAL_DETAIL_SUCCESSED;
			mMessage.obj = UserParser.parserCreditsDetail(result.getResult());
			mHandler.sendMessage(mMessage);
		}else if (command.equals(LCommands.GET_EXCHANGE_LIST)) {
			mMessage.what = GET_EXCHANGE_LIST_SUCCESSED;
			mMessage.obj = UserParser.parserExchangeList(result.getResult());
			mHandler.sendMessage(mMessage);
		}else if (command.equals(LCommands.APPLY_EXCHANGE)) {
			mMessage.what = APPLY_EXCHANGE_SUCCESSED;
			mMessage.obj = result.getDescription();
			mHandler.sendMessage(mMessage);
		}

	}

	@Override
	public void requestFailed(HttpResult result) {
		if (mHandler == null)
			return;
		// 请求失败,result的返回码可判断失败原因，进行如下操作
		mMessage.what = LHttpRequest.REQUEST_FAILED;
		mMessage.obj = result.getDescription();
		mHandler.sendMessage(mMessage);
	}

}
